#include "adc_res.h"

void adc_init(void)
{
	GPIO_InitTypeDef c_init;
	ADC_InitTypeDef adc;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_ADC1, ENABLE);
	
	c_init.GPIO_Pin = GPIO_Pin_0;
	c_init.GPIO_Mode = GPIO_Mode_AIN;
	c_init.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &c_init);
	
	adc.ADC_ContinuousConvMode = DISABLE;
	adc.ADC_DataAlign = ADC_DataAlign_Right;
	adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	adc.ADC_Mode = ADC_Mode_Independent;
	adc.ADC_NbrOfChannel = ADC_Channel_10;
	adc.ADC_ScanConvMode = DISABLE;
	ADC_Init(ADC1, &adc);
	
	ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_239Cycles5);
	
	ADC_Cmd(ADC1, ENABLE);
	
}

u16 get_adc_value(void)
{
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);
	
	while (ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);
	
	ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
	
	return ADC_GetConversionValue(ADC1);
}
